﻿using Common.Contract;
using Common.Contract.ProductHandbook;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Service;
using Common.Service.DataTables;
using Common.Type;
using SMECustomerWebsite.Core.Configuration;
using SMECustomerWebsite.Models.ViewModels.AdminChecklist;
using SMECustomerWebsite.Models.ViewModels.ProductHandbook;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SMECustomerWebsite.Controllers
{
    public class AdminChecklistController : ControllerBase
    {
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult ProductProgram()
        {
            return View();
        }
        [Authorize]
        public ActionResult ViewProductProgram(string productId)
        {
            ProductHandbookChecklistViewModel vm = new ProductHandbookChecklistViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistQuestionByProductRequest request = new RetrieveChecklistQuestionByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                    },
                    Lang = CurrentLang,
                    ProductId = productId,
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveChecklistQuestionByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistQuestionByProduct(request);
                vm.listQuestion = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistQuestionRecord>() : customerRecords.Records;
            }
            return View(vm);
        }
        [Authorize]
        public ActionResult QuestionChecklist(string productId)
        {
            QuetionChecklistViewModel vm = new QuetionChecklistViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistQuestionByProductRequest request = new RetrieveChecklistQuestionByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                    },
                    Lang = CurrentLang,
                    ProductId = productId,
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveChecklistQuestionByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistQuestionByProduct(request);
                vm.listQuestion = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistQuestionRecord>() : customerRecords.Records;
                vm.ProductID = productId;
            }
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult QuestionChecklist(QuetionChecklistViewModel vm, FormCollection form)
        {
            try
            {
                using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
                {
                    ResponseRecord response = null;
                    UpdateChecklistQuestionRequest request = new UpdateChecklistQuestionRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.EditAccountPlanning"
                        },
                        DaoChange = CurrentUserRecord,
                        Record = new ChecklistQuestionRecord
                        {
                            QuestionId = vm.questionEdit.QuestionId,
                            Title = vm.questionEdit.Title,
                            TitleEn = vm.questionEdit.TitleEn,
                            ProductId = int.Parse(vm.ProductID),
                        }
                    };
                    response = serviceClient.Proxy.UpdateQuestion(request);
                    if (response.IsSuccess)
                    {
                        return RedirectToAction("QuestionChecklist", "AdminChecklist", new { productId = vm.ProductID });
                    }
                    else
                    {
                        vm.Message = response.ErrorMessage;
                        vm.MessageType = MessageType.Error;
                        //vm.IsSupperAdmin = CurrentUser.Equals(SystemConfiguration.SUPPER_ADMIN, StringComparison.OrdinalIgnoreCase);
                    }
                    //record = customerRecords.Records.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                vm.MessageType = MessageType.Error;
                vm.Message = ex.ToString();
            }
            return View(vm.ProductID);
        }
        [Authorize]
        public ActionResult RuleChecklist(string productId)
        {
            QuetionChecklistViewModel vm = new QuetionChecklistViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistQuestionByProductRequest request = new RetrieveChecklistQuestionByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                    },
                    Lang = CurrentLang,
                    ProductId = productId,
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveChecklistQuestionByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistQuestionByProduct(request);
                vm.listQuestion = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistQuestionRecord>() : customerRecords.Records;
                vm.ProductID = productId;
                vm.listSubProduct = customerRecords.ListSubProduct;
            }
            return View(vm);
        }
        [Authorize]
        public ActionResult HideDocChecklist(string productId)
        {
            QuetionChecklistViewModel vm = new QuetionChecklistViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistQuestionByProductRequest request = new RetrieveChecklistQuestionByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                    },
                    Lang = CurrentLang,
                    ProductId = productId,
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveChecklistQuestionByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistQuestionByProduct(request);
                vm.listQuestion = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistQuestionRecord>() : customerRecords.Records;
                vm.ProductID = productId;
                vm.listSubProduct = customerRecords.ListSubProduct;
            }
            return View(vm);
        }

        [Authorize]
        public ActionResult DocumentChecklist(string productId)
        {
            DocumentChecklistViewModel vm = new DocumentChecklistViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistProductInfoRequest request = new RetrieveChecklistProductInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                    },
                    Lang = CurrentLang,
                    ProductId = int.Parse(productId),
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveChecklistProductInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistProductInfo(request);
                vm.productInfo = (customerRecords == null || customerRecords.Record == null) ? new ChecklistProductInfoRecord() : customerRecords.Record;
            }
            return View(vm);
        }

        [Authorize]
        public ActionResult DocumentChecklistA2(string productId)
        {
            DocumentChecklistViewModel vm = new DocumentChecklistViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistProductInfoRequest request = new RetrieveChecklistProductInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                    },
                    Lang = CurrentLang,
                    ProductId = int.Parse(productId),
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveChecklistProductInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistProductInfo(request);
                vm.productInfo = (customerRecords == null || customerRecords.Record == null) ? new ChecklistProductInfoRecord() : customerRecords.Record;
            }
            return View(vm);
        }


        [Authorize]
        [HttpPost]
        public ActionResult DocumentChecklist(DocumentChecklistViewModel vm, FormCollection form)
        {
            try
            {
                using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
                {
                    ResponseRecord response = null;
                    UpdateDocumentChecklistRequest request = new UpdateDocumentChecklistRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.EditAccountPlanning"
                        },
                        DaoChange = CurrentUserRecord,
                        Record = new ChecklistDocumentRecord
                        {
                            Document = vm.documentEdit.Document,
                            Copy = vm.documentEdit.Copy,
                            Duplicate = vm.documentEdit.Duplicate,
                            Original = vm.documentEdit.Original,
                            Note = vm.documentEdit.Note,
                            PictureDoc = vm.documentEdit.PictureDoc,
                            Id = vm.documentEdit.Id,
                            SubProductId = vm.documentEdit.SubProductId,
                            ProductId = vm.productInfo.ProductId,
                            DocumentGroupCategory = vm.documentEdit.DocumentGroupCategory,
                            DocumentGroup = vm.documentEdit.DocumentGroup
                        }
                    };
                    response = serviceClient.Proxy.UpdateDocument(request);
                    if (response.IsSuccess)
                    {
                        return RedirectToAction("DocumentChecklist", "AdminChecklist", new { productId = vm.productInfo.ProductId });
                    }
                    else
                    {
                        vm.Message = response.ErrorMessage;
                        vm.MessageType = MessageType.Error;
                        //vm.IsSupperAdmin = CurrentUser.Equals(SystemConfiguration.SUPPER_ADMIN, StringComparison.OrdinalIgnoreCase);
                    }
                    //record = customerRecords.Records.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                vm.MessageType = MessageType.Error;
                vm.Message = ex.ToString();
            }
            return View(vm.productInfo.ProductId);
        }

        [Authorize]
        [HttpPost]
        public ActionResult RuleChecklistData(DataTablesPageRequest pageRequest, string productId)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistRulesByProductRequest request = new RetrieveChecklistRulesByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.Checklist"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    ProductId = int.Parse(productId)
                };
                var customerRecords = new RetrieveChecklistRulesByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistRulesByProduct(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistRulesRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.SubProductName,
                                                             x.AnswerGroup,
                                                             x.Question,
                                                             x.QuestionAnswer,
                                                             x.AnswerGroup
                        }).ToList()
                };
            }
            return Json(pageResponse);
        }
        [Authorize]
        [HttpPost]
        public ActionResult HideDocChecklistData(DataTablesPageRequest pageRequest, string productId)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistHideDocRequest request = new RetrieveChecklistHideDocRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.Checklist"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    ProductId = int.Parse(productId)
                };
                var customerRecords = new RetrieveChecklistHideDocResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistHideDocByProduct(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistHideDocRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.SubProductName,
                                                             x.Question,
                                                             x.Answer,
                                                             x.Document,
                                                             x.RuleHideId
                        }).ToList()
                };
            }
            return Json(pageResponse);
        }
        [Authorize]
        [HttpPost]
        public ActionResult ChecklistDocumentData(DataTablesPageRequest pageRequest, string productid, string routing)
        {

            DataTableResponse pageResponse = null;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistDocumentByProductRequest request = new RetrieveChecklistDocumentByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.Checklist"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    ProductId = productid,
                    SubProductId = routing
                };
                var customerRecords = new RetrieveChecklistDocumentByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistDocumentByProduct(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistDocumentRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.DocumentGroupCategory,
                                                             x.DocumentGroup,
                                                             x.Document,
                                                             x.Original,
                                                             x.Copy,
                                                             x.Duplicate,
                                                             x.Note,
                                                             x.PictureDoc,
                                                             x.Id
                        }).ToList()
                };
            }
            return Json(pageResponse);
        }


        [Authorize]
        [HttpPost]
        public ActionResult ProductProgramData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveProductHandbookCategoryRequest request = new RetrieveProductHandbookCategoryRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ByProductPro"
                    },
                    Lang = CurrentLang,
                };
                var customerRecords = new RetrieveProductHandbookCategoryResponse();
                customerRecords = serviceClient.Proxy.RetrieveProductProgramHandbookCategory(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ProductHandbookCategoryRecord>() : customerRecords.Records;
                List<ProductHandbookProductRecord> listProduct = new List<ProductHandbookProductRecord>();
                resultCustomer.ForEach(item =>
                {
                    listProduct.AddRange(item.Product.Select(x => new ProductHandbookProductRecord
                    {
                        Category = item.Category,
                        ProductId = x.ProductId,
                        ProductName = x.ProductName,
                        HasData = x.HasData
                    }));
                });
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    sColumns = pageRequest.ColumnNames,
                    aaData = listProduct.Select(x => new object[]{
                                                             x.Category,
                                                             x.ProductName,
                                                             x.HasData,
                                                             x.ProductId
                        }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        public ActionResult SaveUploadedFile(string ProductId)
        {
            bool isSavedSuccessfully = true;
            string fName = "";
            try
            {
                foreach (string fileName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[fileName];
                    //Save file content goes here
                    fName = file.FileName;
                    if (file != null && file.ContentLength > 0)
                    {
                        var originalDirectory = new DirectoryInfo(string.Format("{0}Scripts/vendors/fancyImage/{1}/", Server.MapPath(@"\"), ProductId));

                        string pathString = System.IO.Path.Combine(originalDirectory.ToString());

                        var fileName1 = Path.GetFileName(file.FileName);

                        bool isExists = System.IO.Directory.Exists(pathString);

                        if (!isExists)
                            System.IO.Directory.CreateDirectory(pathString);

                        var path = string.Format("{0}\\{1}", pathString, file.FileName);
                        file.SaveAs(path);

                    }

                }

            }
            catch (Exception ex)
            {
                fName = ex.Message;
                isSavedSuccessfully = false;
            }


            if (isSavedSuccessfully)
            {
                return Json(new { Message = fName });
            }
            else
            {
                return Json(new { Message = "Error in saving file" });
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult DeleteUploadedFile()
        {
            bool isSavedSuccessfully = true;
            string fName = "";
            try
            {
                string fullPath = Request.MapPath("~/Images/Cakes/" + fName);
                if (System.IO.File.Exists(fullPath))
                {
                    System.IO.File.Delete(fullPath);
                }

            }
            catch (Exception ex)
            {
                fName = ex.Message;
                isSavedSuccessfully = false;
            }


            if (isSavedSuccessfully)
            {
                return Json(new { Message = fName });
            }
            else
            {
                return Json(new { Message = "Error in saving file" });
            }
        }
        [Authorize]
        [HttpPost]
        public ActionResult DocInfo(string docId)
        {
            ChecklistDocumentRecord resultCustomer = new ChecklistDocumentRecord();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveDocumentInfoByDocIdRequest request = new RetrieveDocumentInfoByDocIdRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.RepreInfo"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    docId = int.Parse(docId)
                };
                var customerRecords = new RetrieveDocumentInfoByDocIdResponse();
                customerRecords = serviceClient.Proxy.RetrieveDocumentInfoByDocId(request);
                resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new ChecklistDocumentRecord() : customerRecords.Record;
            }
            var objectItem = new
            {
                docGroup = resultCustomer.DocumentGroup,
                docGroupCat = resultCustomer.DocumentGroupCategory,
                document = resultCustomer.Document,
                note = resultCustomer.Note,
                copy = resultCustomer.Copy,
                original = resultCustomer.Original,
                duplicate = resultCustomer.Duplicate,
                pictureSample = resultCustomer.PictureDoc
            };
            return Json(objectItem);
        }

        [Authorize]
        [HttpPost]
        public ActionResult AddInfoProduct(string proId)
        {
            bool resultCustomer = false;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                AddProductInfoRequest request = new AddProductInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.RepreInfo"
                    },
                    CurrentDao = CurrentUserRecord,
                    Lang = CurrentLang,
                    Record = new ProductHandbookProductRecord
                    {
                        ProductId = int.Parse(proId)
                    }
                };
                var customerRecords = new ResponseRecord();
                customerRecords = serviceClient.Proxy.AddProductChecklistInfo(request);
                resultCustomer = customerRecords.IsSuccess;
            }
            return Json(resultCustomer);
        }

        [Authorize]
        [HttpPost]
        public ActionResult UpdateRule(string dataedit, string ruleid, string productId, string subId)
        {
            bool isUpdate = false;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                ResponseRecord response = null;
                UpdateChecklistRuleRequest request = new UpdateChecklistRuleRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.AdminChecklistController.UpdateRule"
                    },
                    DataUpdate = dataedit,
                    DaoChange = CurrentUserRecord,
                    RuleId = ruleid,
                    ProductId = productId,
                    SubProductId = int.Parse(subId)
                };
                response = serviceClient.Proxy.UpdateChecklistRule(request);
                isUpdate = response.IsSuccess;
            }
            return Json(isUpdate);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Ruleinfo(string questionid)
        {

            List<ChecklistRulesRecord> resultCustomer = new List<ChecklistRulesRecord>();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveRuleChecklistByIdRequest request = new RetrieveRuleChecklistByIdRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.RepreInfo"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    RuleId = int.Parse(questionid)
                };
                var customerRecords = new RetrieveRuleChecklistByIdResponse();
                customerRecords = serviceClient.Proxy.RetrieveRuleChecklistById(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistRulesRecord>() : customerRecords.Records;
            }
            var objectItem = resultCustomer.Select(x => new
            {
                id = x.QuestionId,
                answer = x.QuestionAnswer,
                subId = x.SubProductId
            }).ToList();
            return Json(objectItem);
        }

        [HttpPost]
        [Authorize]
        public ActionResult HideDocInfo(string questionid)
        {
            ChecklistHideDocRecord resultCustomer = new ChecklistHideDocRecord();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveRuleHideDocByIdRequest request = new RetrieveRuleHideDocByIdRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.RepreInfo"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    RuleId = int.Parse(questionid)
                };
                var customerRecords = new RetrieveRuleHideDocByIdResponse();
                customerRecords = serviceClient.Proxy.RetrieveRuleHideDocById(request);
                resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new ChecklistHideDocRecord() : customerRecords.Record;
            }
            var objectItem = new
            {
                id = resultCustomer.QuestionId,
                answer = resultCustomer.Answer,
                subId = resultCustomer.SubProductId,
                docId = resultCustomer.Document
            };
            return Json(objectItem);
        }
        [HttpPost]
        [Authorize]
        public ActionResult ListDocument(string type, string productId)
        {
            List<ChecklistDocumentRecord> resultCustomer = new List<ChecklistDocumentRecord>();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistDocumentByProductRequest request = new RetrieveChecklistDocumentByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.Checklist"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    ProductId = productId,
                    SubProductId = type
                };
                var customerRecords = new RetrieveChecklistDocumentByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistDocumentByProduct(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistDocumentRecord>() : customerRecords.Records;
            }
            var objectItem = resultCustomer.Select(x => new
            {
                id = x.Id,
                text = x.Document
            });
            return Json(objectItem);
        }
        [HttpPost]
        [Authorize]
        public ActionResult UpdateRuleHideDoc(string dataedit, string ruleid, string productId, string subId)
        {
            bool isUpdate = false;
            string error = string.Empty;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                ResponseRecord response = null;
                UpdateChecklistRuleHideDocRequest request = new UpdateChecklistRuleHideDocRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.AdminChecklistController.UpdateRule"
                    },
                    DataUpdate = dataedit,
                    DaoChange = CurrentUserRecord,
                    RuleId = ruleid,
                    ProductId = productId,
                    SubProductId = int.Parse(subId)
                };
                response = serviceClient.Proxy.UpdateChecklistRuleHideDoc(request);
                isUpdate = response.IsSuccess;
                error = response.ErrorMessage;
            }
            if (isUpdate)
            {
                return Json(isUpdate);
            }
            else
            {
                return Json(error);
            }

        }
    }
}